/*
*
* Copyright (C) 2010, dbzhang800
* All rights reserved.
*
*/

#include <windows.h>
#include <string.h>
#include <stdio.h>

char *strrev(char *str)
{
    for (char *p1=str, *p2=str+strlen(str)-1; p2>p1; ++p1, --p2)
    {
        *p1 ^= *p2;
        *p2 ^= *p1;
        *p1 ^= *p2;
    }
    return str;
}

int main()
{
    HANDLE hFile = CreateFileW(L"test.txt",
            GENERIC_WRITE | GENERIC_READ,
            0,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
        return -1;
    }

    DWORD dwFileSize = GetFileSize(hFile, NULL);

    HANDLE hFileMap = CreateFileMappingW(hFile,
            NULL,
            PAGE_READWRITE,
            0,
            dwFileSize+sizeof(WCHAR),
            NULL);
    if (hFileMap == NULL) {
        CloseHandle(hFile);
        return -2;
    }

    PVOID pvFile = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);
    if (pvFile == NULL) {
        CloseHandle(hFileMap);
        CloseHandle(hFile);
        return -3;
    }

    char * pData = (char*)pvFile;
    pData[dwFileSize]=0;
    strrev(pData);

    printf("%s", (char*)pData);

    UnmapViewOfFile(pvFile);
    CloseHandle(hFileMap);
    CloseHandle(hFile);
    return 0;
}
